package org.example.observerPattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 经典的写法
 */
public class Subject {

    private static final Logger LOGGER = LoggerFactory.getLogger(Subject.class);

    private List<Observer> observers = new ArrayList<>();

    public void add(Observer observer) {
        observers.add(observer);
        LOGGER.info("add an observer");
    }

    public void remove(Observer observer) {
        observers.remove(observer);
        LOGGER.info("remove an observer");
    }

    public void notifyObservers(String newState) {
        for (Observer observer : observers) {
            observer.update(newState);
        }
    }
}
